package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CompanyGoods;
import com.ruoyi.system.service.ICompanyGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家商品Controller
 * 
 * @author ruoyi
 * @date 2025-08-20
 */
@RestController
@RequestMapping("/system/companyGoods")
public class CompanyGoodsController extends BaseController
{
    @Autowired
    private ICompanyGoodsService companyGoodsService;

    /**
     * 查询商家商品列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyGoods companyGoods)
    {
        startPage();
        companyGoods.setCompanyId(getCompanyId());
        System.out.println("companyGoods="+companyGoods.getCompanyId());
        List<CompanyGoods> list = companyGoodsService.selectCompanyGoodsList(companyGoods);
        return getDataTable(list);
    }

    /**
     * 查询商家商品列表,附带库存信息
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping("/list_stock")
    public TableDataInfo list_stock(CompanyGoods companyGoods)
    {
        //一些情况不需要分页数据
        if (StringUtils.isEmpty(companyGoods.getNoPage())){
            startPage();
        }
        companyGoods.setCompanyId(getCompanyId());
        List<CompanyGoods> list = companyGoodsService.selectCompanyGoodsList_stock(companyGoods);
        return getDataTable(list);
    }

    /**
     * 导出商家商品列表
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "商家商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompanyGoods companyGoods)
    {
        companyGoods.setCompanyId(getCompanyId());
        List<CompanyGoods> list = companyGoodsService.selectCompanyGoodsList(companyGoods);
        ExcelUtil<CompanyGoods> util = new ExcelUtil<CompanyGoods>(CompanyGoods.class);
        util.exportExcel(response, list, "商家商品数据");
    }

    /**
     * 获取商家商品详细信息
     */
    @PreAuthorize("@ss.hasRole('company')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(companyGoodsService.selectCompanyGoodsById(id));
    }

    /**
     * 新增商家商品
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "商家商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompanyGoods companyGoods)
    {
        companyGoods.setCompanyId(getCompanyId());
        return toAjax(companyGoodsService.insertCompanyGoods(companyGoods));
    }

    /**
     * 修改商家商品
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "商家商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyGoods companyGoods)
    {
        companyGoods.setCompanyId(getCompanyId());
        return toAjax(companyGoodsService.updateCompanyGoods(companyGoods));
    }

    /**
     * 删除商家商品
     */
    @PreAuthorize("@ss.hasRole('company')")
    @Log(title = "商家商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(companyGoodsService.deleteCompanyGoodsByIds(ids));
    }
}
